<?php
class Mysql{
    const MASTER= 'master';
    const SLAVE= 'slave';
    private static $instance;
    private $dsn = null;
    private $username = null;
    private $password = null;
    private $timeout = null;
    private $charset = null;
    private $conns = array();
    private $stmt=null;
    private $conn=null;

    private static $options = array(
        PDO::ATTR_AUTOCOMMIT            => true,
        PDO::ATTR_ERRMODE               => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE    => PDO::FETCH_ASSOC,
        //PDO::ATTR_PERSISTENT            => true,
    );

    private function __construct($dsn, $username, $password, $timeout = 10, $charset = 'utf8'){
        $this->dsn = $dsn;
        $this->username = $username;
        $this->password = $password;
        $this->timeout = intval($timeout);
        $this->charset = $charset;
    }
    public function getConnection($id = self::MASTER){
        $conn = $this->getCachedConn($id);
        if ($conn) {
            return $conn;
        }
        $opts = self::$options;
        if ($this->timeout > 0) {
            $opts[PDO::ATTR_TIMEOUT] = $this->timeout;
        }
        $username = $this->username->get($id);
        $password = $this->password->get($id);
        $conn = new PDO($this->dsn->get($id), $username, $password, $opts);
        $this->cacheConn($id, $conn);
        if ($this->charset) {
            $conn->exec('set names ' . $this->charset);
        }
        return $conn;
    }
    public static function getInstance($config){
        if(self::$instance==null) {
            self::$instance=new self($config->dsn,$config->username,$config->password);
        }
        return self::$instance;
    }
    public function execute($sql, $params = array())
    {
        $cmd = substr($sql, 0, 6);
        if (strcasecmp($cmd, 'select') === 0) {
            $conn = $this->getConnection(self::SLAVE);
        } else {
            $conn = $this->getConnection(self::MASTER);
        }
        $stmt = $conn->prepare($sql);
        $execStatus=$stmt->execute($params);
        $this->stmt = $stmt;
        $this->conn = $conn;
        return $execStatus;
    }
    public function fetch(){
        return $this->stmt->fetch();
    }
    public function fetchAll(){
        return $this->stmt->fetchAll();
    }
    public function lastInsertId(){
        return $this->conn->lastInsertId();
    }
    public function rowCount(){
        return $this->stmt->rowCount();
    }
    private function getCachedConn($id){
        if (!isset($this->conns[$id])) {
            return null;
        }
        list($conn, $timeout) = $this->conns[$id];
        if (time() < $timeout) {
            $this->cacheConn($id, $conn);
            return $conn;
        } else {
            return null;
        }
    }
    private function cacheConn($id, $conn){
        $timeout = time();
        if ($this->timeout) {
            $timeout += $this->timeout;
        } else {
            $timeout += 30;
        }
        $this->conns[$id] = array($conn, $timeout);
    }
}